--[[
 -- *名称:SingleTypeAnalyzer
 -- *描述:单张
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-10
 -- *修改日期:2017-11-10
 ]]
local DdzSuitAnalyzer = require(basePath.."suit/ddzsuitanalyzer");
local SingleTypeAnalyzer = pokercore.class(DdzSuitAnalyzer,false);

local TUPLE_SIZE = 1;

function SingleTypeAnalyzer:getSuitType()
	return DdzCardTypes.SINGLE;
end


--@Override
function SingleTypeAnalyzer:doCheckRule( cards, term, result )
	--print("SingleTypeAnalyzer:doCheckRule getCardNum->",result:getCardNum());
	--print("TUPLE_SIZE->",TUPLE_SIZE)
	return result:getCardNum() == TUPLE_SIZE  ;
end


--@Override
function SingleTypeAnalyzer:doAnalysizeLaizi( cards, term, onlyMaxValue )
	--print("SingleTypeAnalyzer:doAnalysizeLaizi");
	local cardsResult = {};
	for _,card in ipairs(cards) do -- // 设置number
		card:changeCard(card:getNumber());
	end
	local cardsCopy = cards:clone();	
	self:analysize(cardsCopy,term);

	table.insert(cardsResult,cardsCopy);
	return cardsResult;
end

local MAX_USE_LAIZI_CARD_NUM = 1 ;

--@Override
function SingleTypeAnalyzer:containsPossibleCards(handCards,term)
	return handCards:getResult():getCardNum() >= TUPLE_SIZE;
end


	
--@Override
function SingleTypeAnalyzer:doListPossibleCards(handCards, term, onlyMaxValue) 
	local cardsResult = {} ;
	local result = handCards:getResult();
	local noneLaiziCardList = handCards:subList(1, result:getNoneCardNum());
	local noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
	noneLaiziCards:setResult(handCards:getResult():clone())
	-- --// 非癞子部分寻找单张
	--noneLaiziCards:setResult(handCards:getResult():clone());
	-- table.addAll(cardsResult,DdzTypeUtil.selectSingleCards(noneLaiziCards, true));
	-- if (result:getLaziCardNum() >= 1) then --// 考虑癞子
	-- 	local laiziCardList = handCards:subList(result:getNoneCardNum(), result:getCardNum());
	-- 	local laiziCards = DdzTypeUtil.createCloneCards(laiziCardList);
	-- 	--// 癞子部分寻找单张
	-- 	laiziCards:setResult(handCards:getResult():clone());
	-- 	local singleLaiziCardList = DdzTypeUtil.selectSingleCards(laiziCards, false);
	-- 	for _, cardstemp in ipairs(singleLaiziCardList) do
	-- 		for _, cardtemp in ipairs(cards) do
	-- 			card:changeCard(card:getNumber());
	-- 		end
	-- 	end
	-- 	table.addAll(cardsResult,singleLaiziCardList)
	-- end

	--// 癞子部分
	local laiziCardList = handCards:subList(result:getNoneCardNum()+1, result:getCardNum());
	local laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
	laiziCards:setResult(handCards:getResult():clone());
	
	local laiziCardNum = result:getLaziCardNum();
	local maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum and MAX_USE_LAIZI_CARD_NUM or laiziCardNum;
	for useLaiziCardNum = 0,maxUseLaiziCardNum do
		local needCardNum = TUPLE_SIZE - useLaiziCardNum;
		if (needCardNum > 0) then -- // 部分用癞子的情况
			local useLaiziCardList = DdzTypeUtil.cloneSublistedCards(laiziCards, 1, useLaiziCardNum);--// 补癞子
			local cardsList = DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum);
			for _,usecards in ipairs(cardsList) do
				local card = usecards:get(1);
				for _,laiziCard in ipairs(useLaiziCardList) do
					laiziCard:changeCard(card:getValue());
				end
				local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
				if (self:match(cardsCopy, term)) then
					self:analysize(cardsCopy, term);
					table.insert(cardsResult,cardsCopy);
				end
			end
	    else -- // 全部用癞子的情况
			local useLaiziCardList = DdzTypeUtil.selectTupleCountCards(laiziCards, TUPLE_SIZE);
			for _,useLaiziCards in ipairs(useLaiziCardList) do
				for _,card in ipairs(useLaiziCards) do
					card:changeCard(card:getNumber());
				end
				local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(useLaiziCards, nil);
				if (self:match(cardsCopy, term)) then
					self:analysize(cardsCopy, term);
					table.insert(cardsResult,cardsCopy);
				end
			end
		end
	end
	--print("SingleTypeAnalyzer:doListPossibleCards");
	return cardsResult;
end

return SingleTypeAnalyzer;

